import { Component, OnInit } from '@angular/core';
import { Observable } from "rxjs/Observable";
import { BladeContainerBase } from "../../../../../shared/blade/blade-container-base";
import { FormBuilder, FormGroup, Validators, FormControl } from "@angular/forms";
import { BladeData } from "../../../../../shared/blade/blade-data";
import { Blade } from "../../../../../shared/blade/blade.model";
import { Command } from "../../../../../shared/toolbar.service";
import { ModelParam } from "../../../../../entity/model-Param";
import { ModelParamService } from "../../../../../service/model-param.service";
//import { Setofvalue } from "../../../../../entity/setofvalue.model";
import { ModelParamValueService } from "../../../../../service/model-param-value.service";
import { ModelParamValue } from "../../../../../entity/model-Param-value";
@Component({
  selector: 'app-editor-parameter-edit',
  templateUrl: './editor-parameter-edit.component.html',
  styleUrls: ['./editor-parameter-edit.component.css']
})
export class EditorParameterEditComponent extends BladeContainerBase<ModelParam> {
  /**
    * 风机参数值
    * 
    * @type {ModelParamValue}
    * @memberof EditorParameterEditComponent
    */
  paramValue: ModelParamValue;
  constructor(
    private fb: FormBuilder,
    private modelParamSer: ModelParamService,
    private modelParamValueService: ModelParamValueService
  ) {
    super(fb);
  }

  ngOnInit() {
    this.commands = [
      new Command("save", "保存", "fa fa-save", "setofkey:update", (blade) => this.exeSaveChange(blade), (blade) => this.canSaveChange(blade)),
      new Command("reset", "重置", "fa fa-save", "setofkey:reset", (blade) => this.exeReset(blade), (blade) => this.canReset(blade)),
    ];
    super.ngOnInit();
  }
  initData(): any | Observable<any> | Promise<any> {

    this.formPatchValue(this.currentEntity);

  }
  exeSaveChange(blade: any): void {
    this.saveChange();
  }
  canSaveChange(blade: any): boolean {
    return this.hasChange() && !this.hasError();
  }
  exeReset(blade: any): void {
    return this.formGroup.reset(this.currentEntity);
  }
  canReset(blade: any): boolean {
    return this.hasChange();
  }
  createForm() {
    const obj: {} = {
      paramValue: [this.currentEntity.paramValue,
      [
       /*  Validators.required,
        Validators.pattern("^(-)?[0-9]+([.]{1}[0-9]+){0,1}$"), */
        Validators.maxLength(20),
      ]]
    };
    this.formGroup = this.fromBuider.group(obj);
  }
  /**
  * 设置表单错误对象
  * 
  * 
  * @memberof BladeContainerBase
  */
  setFormError(): void {
    this.formErrors = {
      paramValue: "",
    };
  }
  /**
  * 设置表单验证信息
  * 
  * 
  * @memberof BladeContainerBase
  */
  setValidationMessages(): void {
    this.validationMessages = {
      paramValue: {
        /* 'required': '参数值能为空。',
        'pattern': '参数值必须由数组组成。', */
        'maxlength': '参数值不能多于20个字符长度。',
      },
    };
  }


  /**
   * 
   * 
   * 
   * @memberof TagsEditComponent
   */
  saveChange(): any | Promise<any> | Observable<any> {

    if (this.canSaveChange(this.currentBlade)) {
      this.bladeData.isLoading = true;
      const formValue = this.formGroup.value;
      this.currentEntity.paramValue = formValue.paramValue;
      this.paramValue = {
        paramValueId: this.currentEntity.paramValueId,
        paramValue: this.currentEntity.paramValue,
        modelId: this.currentEntity.modelId ? this.currentEntity.modelId : this.parentEntity.modelId,
        paramId: this.currentEntity.paramId,
      };
      if (this.currentEntity.paramValueId) {
        return this.modelParamValueService.update(this.paramValue)
          .then(res => {
            if (res) {
              (this.parentBlade.bladeContainerInstance).refresh(res);
              this.bladeService.closeBlade(this.currentBlade);
              return true;
            }
            throw new Error("数据更新失败！");
          })
          .catch(error => this.bladeData.error = error)
          .then(res => this.bladeData.isLoading = false);
      } else {
        return this.modelParamValueService.create(this.paramValue)
          .then(res => {
            if (res) {
              (this.parentBlade.bladeContainerInstance).refresh(res);
              this.bladeService.closeBlade(this.currentBlade);
              return true;
            }
            throw new Error("数据更新失败！");
          })
          .catch(error => this.bladeData.error = error)
          .then(res => this.bladeData.isLoading = false);
      }

    }
  }
  changeCurrent() {
    this.bladeData.isLoading = true;
    //console.log(this.currentEntity)
    /* const index: number = this.results.findIndex(o => o.modelParamName == this.brand);
    if (index <= -1) { return false; };
    this.currentEntity.modelParamName = this.results[index].modelParamName;
    this.currentEntity.modelParamKey = this.results[index].modelParamKey;
    this.currentEntity.paramId = this.results[index].paramId;
    const formValue = this.formGroup.value;
    this.currentEntity.paramValue = formValue.paramValue;
    this.paramValue = {
      paramValueId: this.currentEntity.paramValueId,
      paramValue: this.currentEntity.paramValue,
      modelId: this.currentEntity.modelId,
      paramId: this.currentEntity.paramId,
    }; */
  }


}



// WEBPACK FOOTER //
// ./src/app/module/basic/model/editor-parameter/editor-parameter-edit/editor-parameter-edit.component.ts